package com.hengxinguotong.zhihuichengjian.widget.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.hengxinguotong.zhihuichengjian.R;


public class CustomPatient extends Dialog {
	public CustomPatient(Context context) {  
        super(context);  
    }  
  
    public CustomPatient(Context context, int theme) {  
        super(context, theme);  
    }  
  
    public static class Builder {
        private Context context;
        private String content;
        private String title;
        private Typeface lantypeFace;
        private Typeface typeFace;
        private int message;
        private String positiveButtonText;
        private String negativeButtonText;  
        private View contentView;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
  
        public Builder(Context context) {  
            this.context = context;  
        }
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
        public Builder setContent(String content,Typeface lantypeFace,Typeface typeFace) {
            this.content = content;
            this.lantypeFace = lantypeFace;
            this.typeFace = typeFace;
            return this;
        }

        /**
         * Set the positive button resource and it's listener 
         *  
         * @param positiveButtonText 
         * @return 
         */

        public Builder setPositiveButton(int positiveButtonText,  
                OnClickListener listener) {
            this.positiveButtonText = (String) context  
                    .getText(positiveButtonText);  
            this.positiveButtonClickListener = listener;  
            return this;  
        }  
  
        public Builder setPositiveButton(String positiveButtonText,  
                OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;  
            this.positiveButtonClickListener = listener;  
            return this;  
        }  
  
        public Builder setNegativeButton(int negativeButtonText,  
                OnClickListener listener) {
            this.negativeButtonText = (String) context  
                    .getText(negativeButtonText);  
            this.negativeButtonClickListener = listener;  
            return this;  
        }  
  
        public Builder setNegativeButton(String negativeButtonText,  
                OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;  
            this.negativeButtonClickListener = listener;  
            return this;  
        }  
  
		public CustomPatient create() {  
            LayoutInflater inflater = (LayoutInflater) context  
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
            // instantiate the dialog with the custom Theme  
            final CustomPatient dialog = new CustomPatient(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_layout, null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));


            ((TextView)layout.findViewById(R.id.tv_content)).setText(content);
            ((TextView)layout.findViewById(R.id.tv_content)).setTypeface(typeFace);
            if (positiveButtonText != null) {
                TextView tvPositive =    ((TextView) layout.findViewById(R.id.positiveButton));
                tvPositive.setText(positiveButtonText);
                tvPositive.setTypeface(lantypeFace);
                if (positiveButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.positiveButton))
                            .setOnClickListener(new View.OnClickListener() {  
                                public void onClick(View v) {  
                                    positiveButtonClickListener.onClick(dialog,  
                                            DialogInterface.BUTTON_POSITIVE);  
                                }  
                            });  
                }  
            } else {  
                // if no confirm button just set the visibility to GONE  
                layout.findViewById(R.id.positiveButton).setVisibility(  
                        View.GONE);  
            }  
            // set the cancel button  
            if (negativeButtonText != null) {  
                ((TextView) layout.findViewById(R.id.negativeButton))
                        .setText(negativeButtonText);
                ((TextView) layout.findViewById(R.id.negativeButton))
                        .setTypeface(lantypeFace);
                if (negativeButtonClickListener != null) {  
                    ((TextView) layout.findViewById(R.id.negativeButton))
                            .setOnClickListener(new View.OnClickListener() {  
                                public void onClick(View v) {  
                                    negativeButtonClickListener.onClick(dialog,  
                                            DialogInterface.BUTTON_NEGATIVE);  
                                }  
                            });  
                }  
            } else {  
                // if no confirm button just set the visibility to GONE  
                layout.findViewById(R.id.negativeButton).setVisibility(  
                        View.GONE);  
            }  
            // set the content message  
//            if (message != 0) {
//
//            } else if (contentView != null) {
//                // if no message set
//                // add the contentView to the dialog body
//                ((LinearLayout) layout.findViewById(R.id.content))
//                        .removeAllViews();
//                ((LinearLayout) layout.findViewById(R.id.content))
//                        .addView(contentView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
//            }
            dialog.setCanceledOnTouchOutside(false);
            dialog.setContentView(layout);  
            return dialog;  
            
        }
    }  
    
    
    
    
   
    
    
    
    
    
    
    
    
    
    
    
}
